package com.dingding.manager.modules.dirver.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.manager.common.beans.VehiCheckReason;
import com.dingding.manager.common.beans.driver.Specialuservehicleinfo;
import com.dingding.manager.common.dao.Db;
import com.dingding.manager.modules.dirver.dao.IDriverManageAuditDao;
import com.dingding.manager.modules.dirver.dto.ReqDriverManageAuditDTO;
import com.dingding.manager.modules.dirver.dto.ResDriverManageAuditDTO;
import com.dingding.manager.modules.dirver.sql.VehiCheckReasonSQL;

/**
 * 
 * @Description: 司机审核管理
 * @author ligang
 * @date 2016年12月7日 下午9:03:01
 * @version
 */
@Component("driverManageAuditDao")
public class DriverManageAuditDaoImpl implements IDriverManageAuditDao {

	private static final Logger logger=Logger.getLogger(DriverManageAuditDaoImpl.class);
	/**
	 * 
	 * @Description: 修改审核状态
	 * @author ligang
	 * @param suvStatus
	 * @param uvId    参数说明
	 * @return           返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResDriverManageAuditDTO updateDriverManageState(ReqDriverManageAuditDTO reqDriverManageAuditDTO) {
		try {
			Specialuservehicleinfo specialuservehicleinfo = Db.findById(reqDriverManageAuditDTO.getUvId(), Specialuservehicleinfo.class);
			ResDriverManageAuditDTO resDriverManageAuditDTO = new ResDriverManageAuditDTO();
			//审核状态为2
			if(reqDriverManageAuditDTO.getSuvStatus() == 2){
					specialuservehicleinfo.setSuvStatus(reqDriverManageAuditDTO.getSuvStatus());
					specialuservehicleinfo.setVehicleLevelId(reqDriverManageAuditDTO.getVehicleLevelId());
					specialuservehicleinfo.setThroughTime(new Date());
					Db.saveOrUpdate(specialuservehicleinfo);
					resDriverManageAuditDTO.setResponseCode(200);
					return resDriverManageAuditDTO;
			}
			//审核状态为3并返回未通过原因
			if(reqDriverManageAuditDTO.getSuvStatus() == 3){
				String vridp = reqDriverManageAuditDTO.getVrid();
				if(vridp != "" && vridp != null){
					String[] split = vridp.split(",");
					String descriptions = null;
						for (String vrid : split) {
						String description = Db.findById(vrid, VehiCheckReason.class).getDescription();
						if(description != null){
							descriptions += description+"&";
						}
					}
					descriptions = descriptions.substring(0,descriptions.length()-1);
					specialuservehicleinfo.setDescription(descriptions);
				}
				specialuservehicleinfo.setSuvStatus(reqDriverManageAuditDTO.getSuvStatus());
				Db.saveOrUpdate(specialuservehicleinfo);
				resDriverManageAuditDTO.setResponseCode(400);
				return resDriverManageAuditDTO;
			}
		} catch (RuntimeException e) {
			logger.error("根据司机ID修改审核状态异常",e);
		}
		return null;
	}
	
	/**
	 * 
	 * @Description: 审核管理未通过返回原因
	 * @author ligang
	 * @param @return    参数说明
	 * @return           返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public List<VehiCheckReason> driverManageNotAudit() {
		List<VehiCheckReason> vehiCheckReasons = Db.querySqlList(VehiCheckReason.class, VehiCheckReasonSQL.GET_VEHI_CHECK_REASON);
		return vehiCheckReasons;
	}
	
	

}
